<?php
            
use app\model\Theme; 


/**
 * Author: cbw
 * Desc: 课程分类
 * Time: 2022/5/24 14:25
 * Email: 17854288794@163.com
 */
class Category_Page extends PluginWebPage {
    public function main()
    {
        global $_G,$_P,$_S,$_C;
        $action = 71;
        $themeid = intval($_P['themeid']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        $themelist = Theme::getAllTheme();
        $search_data = isset($_P['search_data'])?$_P['search_data']:'';
        if ($search_data){
            $search_data = '%'.$search_data.'%';
        }
        $page_index = max(1,intval($_P['page']));
        $page_size = 50;
        $page_start = ($page_index-1)*$page_size;
        $page_total = EducationModel::getCategoryList($themeid,$search_data,0,0,true);
        $list = EducationModel::getCategoryList($themeid,$search_data,$page_start,$page_size);
        $first = [];
        foreach ($list as $v){
            if ($v['pid']==0){
                array_push($first,$v);
                $second = EducationModel::getCategoryListBy(['themeid'=>$themeid,'pid'=>$v['ct_id']]);
                foreach ($second as $vv){
                    array_push($first,$vv);
                }
            }
        }
        $item = EducationModel::getRewardByWhere(['themeid'=>$themeid]);
        $open_shangjia_rules = $item['open_shangjia_rules'];
        $pager = pagination($page_total,$page_index,$page_size);

        $item = EducationModel::getRewardByWhere(['themeid'=>$themeid]);
        $course_style = $item['course_style'];
        if ($course_style=='4'){
            $return_clicent = get_defined_vars();
return $this->view('/education/category/course_main', $return_clicent);
        }else{
            $return_clicent = get_defined_vars();
return $this->view('/education/category/main', $return_clicent);
        }

    }

    function getItem()
    {
        global $_G, $_P;
        $themeid = intval($_P['themeid']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        if (!$themeid){
            show_json(0,'活动入口不正确');
            return;
        }
        if ($_G['ispost']){
            $item = EducationModel::getCategory(['themeid'=>$themeid,'ct_id'=>$_P['ct_id']]);
            if (!$item){
                show_json(0,['msg'=>'操作失败','data'=>[]]);
            }else{
                show_json(1,['msg'=>'操作成功','data'=>$item]);
            }
        }
    }

    function edit_child()
    {
        global $_G, $_P;
        $themeid = intval($_P['themeid']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        if (!$themeid){
            show_json(0,'活动入口不正确');
            return;
        }
        if ($_G['ispost']){
            if (isset($_P['ct_id'])){
                $item = EducationModel::getCategory(['themeid'=>$themeid,'ct_id'=>$_P['ct_id']]);
                $list = EducationModel::getCategoryListBy(['themeid'=>$themeid,'pid'=>0]);
                $html = '<option value="0" >------请选择所属一级分类------</option>'.PHP_EOL;
                foreach ($list as $key => $val){
                    if ($val['ct_id']==$item['pid']){
                        $html .= '<option value="'.$val['ct_id'].'" selected>'.$val['ct_name'].'</option>'.PHP_EOL;
                    }else{
                        $html .= '<option value="'.$val['ct_id'].'">'.$val['ct_name'].'</option>'.PHP_EOL;
                    }
                }
            }else{
                $item = [];
                $list = EducationModel::getCategoryListBy(['themeid'=>$themeid,'pid'=>0]);
                $html = '<option value="0" >------请选择所属一级分类------</option>'.PHP_EOL;
                foreach ($list as $key => $val){
                    $html .= '<option value="'.$val['ct_id'].'">'.$val['ct_name'].'</option>'.PHP_EOL;
                }
            }
            show_json(1,['msg'=>'操作成功','data'=>['item'=>$item,'list'=>$html]]);
        }else{

        }
    }

    function edit()
    {
        return $this->post();
    }

    protected function post()
    {
        global $_G, $_P;
        $themeid = intval($_P['themeid']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        if (!$themeid){
            show_json(0,'活动入口不正确');
            return;
        }
        if (!$_G['ispost']){
            show_json(0,'非法请求');
            return;
        }else{
            if (isset($_P['pid']) && $_P['pid']){
                $pid = $_P['pid'];
            }else{
                $pid = 0;
            }
//            if (!$_P['ct_sj']){
//                show_json(0,['msg'=>'最大可选课程数量未设置']);
//                return;
//            }
//            if (!$_P['ct_sj_min']){
//                show_json(0,['msg'=>'最小可选课程数量未设置']);
//                return;
//            }
            if ((int)$_P['ct_sj']<(int)$_P['ct_sj_min']){
                show_json(0,['msg'=>'最小可选课程数量不能大于最大可选课程数量']);
                return;
            }
            if (!isset($_P['ct_id']) || !$_P['ct_id']){
                //新增
                $data = ['ct_sort'=>$_P['ct_sort'],'pid'=>$pid,'ct_name'=>$_P['ct_name'],'ct_sj'=>$_P['ct_sj'],'ct_sj_min'=>$_P['ct_sj_min'],'themeid'=>$themeid];
                $res = EducationModel::insertCategory($data);
            }else{
                //修改
                $where = ['ct_id'=>$_P['ct_id'],'themeid'=>$themeid];
                $data = ['ct_sort'=>$_P['ct_sort'],'ct_name'=>$_P['ct_name'],
                    'pid'=>$pid,'ct_sj'=>$_P['ct_sj'],'ct_sj_min'=>$_P['ct_sj_min']];
                $res = EducationModel::editCategory($where,$data);
            }
            if (!$res){
                show_json(0,['msg'=>'操作失败']);
            }else{
                show_json(1,['msg'=>'操作成功']);
            }
        }
    }

    function del()
    {
        global $_G, $_P;
        $themeid = intval($_P['themeid']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        if (!$themeid){
            show_json(0,'活动入口不正确');
            return;
        }
        if (!$_G['ispost']){
            show_json(0,'非法请求');
            return;
        }else{
            if (!isset($_P['ct_id']) || !$_P['ct_id']){
                show_json(0,'参数缺失');
                return;
            }else{
                //删除
                $where = ['ct_id'=>$_P['ct_id'],'themeid'=>$themeid];
                $res = EducationModel::delCategory($where);
            }
            if (!$res){
                show_json(0,['msg'=>'操作失败']);
            }else{
                show_json(1,['msg'=>'操作成功']);
            }
        }
    }
}